import aiofiles
import os
import time
from datetime import datetime, timedelta
from typing import Annotated, Literal, Union
from uuid import UUID

import uvicorn
from fastapi import FastAPI, Path, Query, Body, Cookie, Header, status, Form, File, UploadFile, HTTPException,Request
from fastapi.encoders import jsonable_encoder
from fastapi.exceptions import RequestValidationError
from fastapi.openapi.docs import get_swagger_ui_html, get_swagger_ui_oauth2_redirect_html, get_redoc_html
from fastapi.responses import HTMLResponse,JSONResponse,PlainTextResponse
from starlette.staticfiles import StaticFiles
from pydantic import BaseModel, Field, HttpUrl, EmailStr

# 创建FastAPI实例，禁用掉FastAPI会自动生成Swagger UI和Redoc文档
app = FastAPI(docs_url=None, redoc_url=None)

# 静态文件挂载（路由与静态文件映射）
app.mount("/static", StaticFiles(directory="../statics"), name="static")


# 重新提供一个Swagger UI，相当于我们自己重写了这么一个路由
@app.get("/docs", include_in_schema=False)
def custom_swagger_ui():
    return get_swagger_ui_html(
        openapi_url = "/openapi.json",
        title = "Swagger UI for FastAPI",
        swagger_js_url="/static/swagger-ui-bundle.min.js",
        swagger_css_url="/static/swagger-ui.min.css"
    )

# 这里与Swagger UI一样，重新提供Redoc
@app.get("/redoc", include_in_schema=False)
def custom_redoc():
    return get_redoc_html(
        openapi_url = "/openapi.json",
        title = "Redoc for FastAPI",
        redoc_js_url="/static/redoc.standalone.js"
    )

class Item(BaseModel):
    name: str
    description: str | None = None
    price: float
    tax: float | None = None
    tags: set[str] = set()

@app.post("/items", response_model=Item, status_code=status.HTTP_201_CREATED)
async def create_item(item: Item):
    return item

@app.post("/items1",response_model=Item, tags=["items","orders"])
async def create_item1(item: Item):
    return item

@app.get(
    "/items2",
    tags=["items"],
    summary="获取items",
    # description="获取items的详细描述",
    response_description="这是关于items的详细描述",
    deprecated=True,
)
async def read_items():
    """
    这是关于read_items()的详细说明:

    **作者：** advent

    :return: 关于item列表的JSON数据
    """

    return [
        {"name": "手机", "price": 2999.0},
        {"name": "电脑", "price": 6999.0, "tax": 20.5},
        {"name": "手表", "price": 1999, "tax": 8}
    ]

@app.get("/order", tags=["orders"])
async def read_order():
    return [
        {"name": "手机", "price": 2999.0},
        {"name": "电脑", "price": 6999.0, "tax": 20.5}
    ]


if __name__ == '__main__':
    app.debug = True
    uvicorn.run("main:app", host="127.0.0.1", port=8021, reload=True)

